# References:
# http://www.swig.org/Doc3.0/SWIGDocumentation.html#Python
# http://www.swig.org/Doc3.0/SWIGDocumentation.html#Introduction_nn4
# https://docs.python.org/2.7/extending/building.html#distributing-your-extension-modules
# https://docs.python.org/2/distutils/setupscript.html#describing-extension-modules
# https://docs.python.org/2/distutils/apiref.html#distutils.core.Extension
# http://pythonhosted.org/setuptools/setuptools.html

from setuptools import setup, Extension
from setuptools.command.install import install
from distutils.command.build import build

# By default, distutils/setuptools build Python before the extensions.
# Unfortunately a Python file is generated when building the SWIG extension.
# That means that the generated file is not going to be installed since the
# Python build has already happened!
# As a workaround, we need to use a custom build and install class that run the
# SWIG extension first.
# References:
# https://stackoverflow.com/questions/12491328/python-distutils-not-include-the-swig-generated-module
# https://stackoverflow.com/questions/17666018/using-distutils-where-swig-interface-file-is-in-src-folder
class BuildSwig(build):
    def run(self):
        self.run_command('build_ext')
        build.run(self)


class InstallSwig(install):
    def run(self):
        self.run_command('build_ext')
        install.run(self)

swig = Extension(
    'swagga.extensions._example',
    ['swagga/extensions/example.i', 'native/code/example.c'],
    include_dirs=['native/include'],
    define_macros=None,
    undef_macros=None,
    library_dirs=None,
    libraries=None,
    runtime_library_dirs=None,
    extra_objects=None,
    extra_compile_args=None,
    extra_link_args=None,
    export_symbols=None,
    swig_opts=None,
    depends=None,
    language=None
)

setup(
    cmdclass={'build': BuildSwig, 'install': InstallSwig},
    name='swagga.extensions',
    packages=['swagga.extensions'],
    namespace_packages=['swagga'],
    install_requires=['setuptools'],
    version='0.1.dev1',
    ext_modules=[swig],
)
